﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Web.Framework;
using Web.Framework.Svc;
using System.Web.UI;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Web.Framework.Translate;
using Web.Framework.Misc;
using AuthApi.Contracts;

namespace WebAppAuthAdmin.Code
{
    public abstract class BasePage : BaseWebPage
    {
        protected WebServiceHelper WebServiceHelper { get; set; }

        protected UIHelper UIHelper { get; set; }

        public Company GetSingleContextCompany()
        {
            Company company = AppContext.Instance.GetSingleCompanyContext(this.Context);
            if (company == null)
            {
                company = CompanySinSeleccionar.Instance;
            }

            return company;
        }

        protected override void OnPreLoad(EventArgs e)
        {
            try
            {
                WebServiceHelper = new WebServiceHelper();
                WebServiceHelper.ServiceOptionsDefault = new ServiceOptions();
                WebServiceHelper.ServiceOptionsDefault.UserName = BaseWebPage.UserName;

                UIHelper = new UIHelper();
                var company = this.GetSingleContextCompany();
                if (company != null)
                {
                    UIHelper.companyId = company.Id;
                }
                else
                {
                    // forzar el picker ya que no se puede trabajar sin empresa (salvo para la empresa que es 'Independiente de la Marca'
                }

                UIHelper.WebServiceHelper = WebServiceHelper;

                base.LoadOperations(WebServiceHelper.GetOperationsForUser(
                    BaseWebPage.ApplicationId, BaseWebPage.CompanyId, BaseWebPage.UserName));

                base.OnPreLoad(e);
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            // Valido que la combinacion aplicación y empresa del contexto tenga sentido
            // si no tiene sentido la redirijo a la lista de aplicaciones

            var company = this.GetSingleContextCompany();
            if (company != null)
            {
                string applicationIStr = this.Request.QueryString["applicationId"];
                if (!String.IsNullOrEmpty(applicationIStr))
                {
                    int applicationId = Convert.ToInt32(applicationIStr);
                    if (!WebServiceHelper.ExistCompanyApplication(company.Id, applicationId))
                    {
                        string mensajeUrlEncoded = Server.UrlEncode("Fue redirigido a esta página porque la aplicación no está configurada para esta marca");
                        this.Response.Redirect("/App/ApplicationList.aspx?msg=" + mensajeUrlEncoded, true);
                    }
                }               
            }

            base.OnLoad(e);
        }

        public void LoadContextActions(List<LinkButton> linkButtons)
        {
            SiteMaster siteMaster = (SiteMaster)this.Master;
            siteMaster.LoadContextActions(linkButtons);
        }
        protected void SetJavascriptForClientClickDirtyChech(LinkButton lnkBtn)
        {
            lnkBtn.OnClientClick = "if(document.DirtyCheck.areControlsDirty()) {return confirm('" + LocalizationProvider.Instance["Bo.Messages.DirtyMessageConfirmContinue"] + "'); } else {return true;}";
        }

        public static List<WebApplicationInfo> GetApplications()
        {
            return WebServiceHelper.GetApplicationList(string.Empty);
        }

        public static List<Company> GetCompanies()
        {
            return WebServiceHelper.GetCompanyList(string.Empty);
        }             
    }
}